package client.gui.panels;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import controllers.ControllerIF;
import controllers.TimeFrameRequestController;

public class TimeFrameRequest extends JPanel implements ActionListener, PanelIF {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Instance of the panel's controller
	 */
	private TimeFrameRequestController timeFrameRequestController;
	
	/**
	 * local reference for the requested time frame
	 */
	private Integer requestedTime;
	
	
	private JTextField daysTextField;
	private JButton okBtn;

	/**
	 * Create the panel.
	 */
	public TimeFrameRequest(String stage) {
		initGUI(stage);
	}
	
	/**
	 * Create the GUI
	 * @param stage String
	 */
	public void initGUI(String stage) {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		add(Box.createVerticalStrut(20));
		
		JPanel promptPanel = new JPanel();
		add(promptPanel);
		promptPanel.setLayout(new BoxLayout(promptPanel, BoxLayout.X_AXIS));
		
		JLabel promptLabel = new JLabel("Set time frame for the " + stage + " stage:");
		promptPanel.add(promptLabel);
		
		promptPanel.add(Box.createHorizontalStrut(20));
		
		daysTextField = new JTextField();
		promptPanel.add(daysTextField);
		daysTextField.setMaximumSize(new Dimension(40, 16));
		daysTextField.setColumns(3);
		
		promptPanel.add(new JLabel(" days."));
		
		add(Box.createVerticalStrut(20));
		
		JPanel buttonsPanel = new JPanel();
		add(buttonsPanel);
		buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.X_AXIS));
		
		buttonsPanel.add(Box.createHorizontalGlue());
		
		okBtn = new JButton("OK");
		buttonsPanel.add(okBtn);
		okBtn.addActionListener(this);
		
		buttonsPanel.add(Box.createHorizontalStrut(20));
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if(ae.getSource().equals(okBtn)) {
			try {
				requestedTime = Integer.parseInt( daysTextField.getText() );
				timeFrameRequestController.action();
			} catch (NumberFormatException nfe) {
				JOptionPane.showMessageDialog(this, "Incorrect input.", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
	
	/**
	 * Get the evaluator's time request
	 * @return Integer
	 */
	public Integer getEvaluatorRequest() { 
		return requestedTime;
	}

	@Override
	public void setController(ControllerIF cif) {
		this.timeFrameRequestController = (TimeFrameRequestController) cif;
	}


	/**
	 * Set all text areas and buttons editable / uneditable according to a given parameter
	 * @param bool boolean
	 */
	public void setPanelEditable(boolean bool) {
		daysTextField.setEditable(bool);
	}
	
}
